$(function(){
	
	var resizeChartContainer = function(location){
		var rightWidth = $("#"+location).width();
		var width = $("#"+location).css("width");
		width = width.replace("px","");
		var padding = Number(width)-rightWidth;
		if(rightWidth>0){
			$("#"+location).css("width",(rightWidth-padding)+"px");
			$("#"+location).css("height",(rightWidth/1.1)+"px");
		}
	};
	resizeChartContainer("section_avg");
	resizeChartContainer("section_article");
	resizeChartContainer("type_count");
	resizeChartContainer("type_avg");
	var section_avg = document.getElementById("section_avg");
	var section_article = document.getElementById("section_article");
	var type_count = document.getElementById("type_count");
	var type_avg = document.getElementById("type_avg");
	var section_avg_chart = echarts.init(section_avg);
	var section_article_chart = echarts.init(section_article);
	var type_count_chart = echarts.init(type_count);
	var type_avg_chart = echarts.init(type_avg);
	
	initSectionChart(section_avg_chart,section_article_chart);
	initTypeChart(type_count_chart,type_avg_chart);
	initCount();
	
	window.onresize = function () {
	    //重置容器高宽
//		resizeChartContainer("section_avg");
//		resizeChartContainer("section_article");
//		resizeChartContainer("type_count");
//		resizeChartContainer("type_avg");
//		section_avg_chart.resize();
//		section_article_chart.resize();
//		type_count_chart.resize();
//		type_avg_chart.resize();
	};
});

function initCount(){
	$.ajax({
		url:BASE_PATH+'/toutiao/count.do',
		type:'get',
		dataType:'json',
		success:function(data){
			$("#article_count").html("文章："+data['article_count']+"篇");
			$("#author_count").html("头条号："+data['author_count']+"个");
		}
	});
}

function initSectionChart(chart,chart1){
	var option = {
	    legend: {
	        data:['各分类平均阅读量']
	    },
	    xAxis: [
	        {
	            type: 'category',
	            data: [],
	            axisLabel:{
	            	rotate:-90,
	            	interval:0
	            }
	        }
	    ],
	    yAxis: [
	        {
	            type: 'value',
	            name: '平均阅读量',
	            min: 0,
	            axisLabel: {
	                formatter: function(data){
	                	return data/10000+"万";
	                }
	            }
	        }
	    ],
	    series: [
	        {
	            name:'各分类平均阅读量',
	            type:'bar',
	            data:[]
	        }
	    ]
	};
	var option1 = {
		    legend: {
		        data:['各分类文章数量']
		    },
		    xAxis: [
		        {
		            type: 'category',
		            data: [],
		            axisLabel:{
		            	rotate:-90,
		            	interval:0
		            }
		        }
		    ],
		    yAxis: [
		        {
		            type: 'value',
		            name: '文章数量',
		            min: 0
		        }
		    ],
		    series: [
		        {
		            name:'各分类文章数量',
		            type:'bar',
		            data:[]
		        }
		    ]
		};
	$.ajax({
		url:BASE_PATH+'/toutiao/section.do',
		type:'get',
		dataType:'json',
		success:function(data){
			var sectionName = [];
			var section_avg_value = [];
			var section_count = [];
			for(var i = 0; i < data.length;i++){
				sectionName.push(data[i]['section']);
				section_avg_value.push(data[i]['readcount_avg']);
				section_count.push(data[i]['section_count']);
			}
			option.xAxis[0].data = sectionName;
			option.series[0].data = section_avg_value;
			chart.setOption(option);
			option1.xAxis[0].data = sectionName;
			option1.series[0].data = section_count;
			chart1.setOption(option1);
		}
	});
}

function initTypeChart(chart,chart1){
	var option = {
	    title : {
	        text: '不同文章类型阅读量比例',
	        x:'center'
	    },
	    legend: {
	        data: []
	    },
	    series : [
	        {
	            name: '不同文章类型阅读量比例',
	            type: 'pie',
	            data:[],
	            label: {
	                normal: {
	                    position: 'inner'
	                }
	            }
	        }
	    ]
	};
	var option1 = {
	    title : {
	        text: '不同文章类型文章数量比例',
	        x:'center'
	    },
	    legend: {
	        data: []
	    },
	    series : [
	        {
	            name: '不同文章类型文章数量比例',
	            type: 'pie',
	            data:[],
	            label: {
	                normal: {
	                    position: 'inner'
	                }
	            }
	        }
	    ]
	};
	$.ajax({
		url:BASE_PATH+'/toutiao/type.do',
		type:'get',
		dataType:'json',
		success:function(data){
			var typeName = [];
			var type_readcount = [];
			var type_count = [];
			var sum = 0;
			var count = 0;
			for(var i = 0; i < data.length;i++){
				var type_name = "";
				if(data[i]['type']==1){
					type_name = "文章";
				}
				if(data[i]['type']==2){
					type_name="图册";
				}
				if(data[i]['type']==3){
					type_name="视频";
				}
				type_readcount.push({"name":type_name+"\n平均阅读量:"+data[i]['readcount_sum'].toFixed(0),"value":data[i]['readcount_sum']});
				type_count.push({"name":type_name+"\n文章数量:"+data[i]['type_count'].toFixed(0),"value":data[i]['type_count']});
				sum += data[i]['readcount_sum'];
				count += data[i]['type_count'];
			}
			for(var i = 0;i < type_readcount.length; i++){
				type_readcount[i]['value'] = type_readcount[i]['value']/sum;
				type_count[i]['value'] = type_count[i]['value']/count;
			}
			option.series[0].data = type_readcount;
			chart.setOption(option);
			option1.series[0].data = type_count;
			chart1.setOption(option1);
		}
	});
}